package org.example.mrrope.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.mrrope.service.OwnDoKnotWorkOrderService;
import org.example.mrrope.utils.DingUtil;
import org.example.mrrope.utils.FormUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 制结&手工 个人派工单
 */
@Service
@Slf4j
public class OwnDoKnotWorkOrderServiceImpl implements OwnDoKnotWorkOrderService {

    @Autowired
    private DingUtil dingUtil;

    @Override
    @Async
    public Integer saveOwnDoKnotWorkOrder(String id) {
        try {
            //得到制结&手工车间派工单数据
            log.info("开始调用宜搭查询接口");
            JSONObject jsonObject = new JSONObject(dingUtil.getYidaById(id));
            log.info("调用查询接口返回制结&手工车间派工单数据" + jsonObject);
            // body
            int res = 1;
            if (jsonObject.has("tableField_lxifktcx")) {
                JSONArray OwnerTable = jsonObject.getJSONArray("tableField_lxifktcx");
                for (int i = 0; i < OwnerTable.length(); i++) {
                    JSONObject ownJson = getUpJson(jsonObject,id);
                    ownJson.put("employeeField_lxifnhdj", OwnerTable.getJSONObject(i).get("employeeField_lxifktcy_id"));
                    ownJson.put("numberField_lxifnhdq", OwnerTable.getJSONObject(i).get("numberField_lxifktcz"));//作业数量
                    ownJson.put("numberField_lzc1uby1", OwnerTable.getJSONObject(i).get("numberField_lxifktcz"));//未上交数
                    ownJson.put("textField_lzc4e1ai", OwnerTable.getJSONObject(i).get("textField_lzc4f0s5"));//工号
                    ownJson.put("dateField_lxifnhdr", OwnerTable.getJSONObject(i).get("dateField_lxifktd0"));
                    String type = OwnerTable.getJSONObject(i).getString("radioField_m2a3jcio");
                    ownJson.put("radioField_lzca13o8", type);//派工单类型
                    //判断派工单类型和第二道工序是否相同
                    String radioFie = ownJson.getString("radioField_m2a426gt");
                    if (type.equals(radioFie) || type.equals("无")){
                        ownJson.put("textField_m2a4rdf9", "入库");//发起流程类型判断
                    }else{
                        ownJson.put("textField_m2a4rdf9", "转交");//发起流程类型判断
                    }
                    ownJson.put("numberField_lzbfqetx", type.equals("制结")?jsonObject.get("numberField_lzc1jw04"):jsonObject.get("numberField_m2a3jcin"));//工价
                    ownJson.put("textField_lxin7fu2","待确认");
                    log.info("*************新建制结&手工车间个人派工单请求参数---->>>>" + ownJson);
                    //李浩
                    String result = dingUtil.startInstance(ownJson, "0563573044847835", "FORM-9D5FE18A160242A7972A845300BB0D738YW4");//李浩
                    log.info("*************新建制结&手工车间个人派工单返回参数---->>>>" + result);
                    if (result != null) {
                        log.info("新建制结&手工车间个人派工单成功，单号====》" + result);
                        res = 0;
                    } else {
                        log.info("新建制结&手工车间个人派工单失败");
                        res = 1;
                    }
                }
//                String upRes = dingUtil.UpdateYiDa(id, jsonObject);
//                log.info("生成制结&手工个人任务后更新结果===》" + upRes + jsonObject);
                return res;
            } else {
                log.info("未找到工人");
                return 1;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }



    private static JSONObject getUpJson(JSONObject jsonObject, String id) {
        JSONObject ownJson = new JSONObject();
        JSONArray jsonArrayOne = FormUtils.getAssociationFormField("FORM-A6E69C4290D3475C8F31A1DF76A03201NPH8", jsonObject.get("textField_lyz6ump9").toString(), id);
        ownJson.put("associationFormField_lxtusqg0", jsonArrayOne);
        ownJson.put("textField_lxy444g8", jsonObject.get("textField_lx1mddjk"));//客户名称
        ownJson.put("textField_lxy444g9", jsonObject.get("textField_lugarhfd"));//半成品B号
        ownJson.put("textField_lxy444g6", jsonObject.get("textField_lugarhfe"));//客人货号
        ownJson.put("imageField_lugarhfm", jsonObject.has("imageField_lugarhfm")?jsonObject.get("imageField_lugarhfm"):"");//成品图片
        ownJson.put("textField_lxifktd1", jsonObject.get("textField_lxifktd1"));//半成品名称
        ownJson.put("textField_lugarhfx", jsonObject.get("textField_lugarhfx"));//规格重量
        ownJson.put("tableField_lxwy3kxi", jsonObject.get("tableField_lxwy3kxi"));//绳子子表
        ownJson.put("radioField_m2a426gs", jsonObject.get("radioField_m2a426gs"));//第一道工序
        ownJson.put("radioField_m2a426gt", jsonObject.get("radioField_m2a426gt"));//第二道工序
        ownJson.put("textField_lugarhgb", jsonObject.get("textField_lugarhgb"));//注意点
        ownJson.put("textField_lywpr91j", jsonObject.get("textField_lyz6ump9"));//SO号
//        ownJson.put("textField_lxy444g5", jsonObject.get("textField_lugarhfc"));//AC号
//        ownJson.put("numberField_lzbfqety", jsonObject.get("numberField_lzavb7sq"));//绳子总成本

        ownJson.put("numberField_lxwv7czn", jsonObject.has("numberField_lxwv7czn")?jsonObject.get("numberField_lxwv7czn"):null);//总应发数量
        ownJson.put("numberField_lzbh4esz", jsonObject.has("numberField_lzbfxx57")?jsonObject.get("numberField_lzbfxx57"):null);//切绳重量
        //
        //配件子表
        if (jsonObject.has("tableField_lvxe56yi")){
            List<JSONObject> listJOSNObject = new ArrayList<>();
            JSONArray jsonArrayTow = jsonObject.getJSONArray("tableField_lvxe56yi");
            for (int j = 0; j < jsonArrayTow.length(); j++) {
                JSONObject ownJsonTow = new JSONObject();
                JSONObject ownJsonB = jsonArrayTow.getJSONObject(j);
                ownJsonTow.put("textField_lx07bljk", ownJsonB.get("textField_lx07bljk"));//半成品B号
                ownJsonTow.put("textField_lujgt59d", ownJsonB.get("textField_lujgt59d"));//配件名称
                ownJsonTow.put("textField_lujgt59f", ownJsonB.get("textField_lujgt59f"));//配件编号
                ownJsonTow.put("textField_lx3ck53r", ownJsonB.get("textField_lx3ck53r"));//规格描述
                ownJsonTow.put("textField_lwg902pd", ownJsonB.has("textField_lwg902pd")?ownJsonB.get("textField_lwg902pd"):"");//供应商
                ownJsonTow.put("imageField_lwg902pf", ownJsonB.has("imageField_lwg902pf")?ownJsonB.get("imageField_lwg902pf"):"");//例图
                ownJsonTow.put("numberField_lxvgkgg8", ownJsonB.has("numberField_lxvgkgg8")?ownJsonB.get("numberField_lxvgkgg8"):"");//标准用量
                ownJsonTow.put("textField_lwee5prf", ownJsonB.has("textField_lwee5prf")?ownJsonB.get("textField_lwee5prf"):"");//备注
                listJOSNObject.add(ownJsonTow);
            }
            ownJson.put("tableField_lvxe56yi",listJOSNObject);
        }
        return ownJson;
    }

}
